package com.zhiyou.service.impl;


import com.zhiyou.dao.RoleMapper;
import com.zhiyou.dao.UserMapper;
import com.zhiyou.dao.UserRoleMapper;
import com.zhiyou.entity.Activity;
import com.zhiyou.entity.User;
import com.zhiyou.entity.UserRole;
import com.zhiyou.service.UserManagerService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserManagerServiceImpl implements UserManagerService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    RoleMapper roleMapper;
    @Autowired
    UserRoleMapper userRoleMapper;

    @Override
    public List<User> getAll(String field,String keyword) {
        List<User> list = null;
        if (field!=null){
            Example example = new Example(Activity.class);
            Example.Criteria criteria = example.createCriteria();
            System.out.println(field+keyword);
            criteria.andLike(field,"%"+keyword+"%");
            list = userMapper.selectByExample(example);
            List<User> list2 = new ArrayList<>();
            for (int i = 0; i < list.size(); i++) {
                if(list.get(i).getuStatus()==1){
                    list2.add(list.get(i));
                }
            }
            return list2;
        }
        list = userMapper.getAll();
        List<User> list3 = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getuStatus()==1){
                list3.add(list.get(i));
            }
        }
        return list3;
    }

    @Override
    public void add(User user) {
        try{
            userMapper.insert(user);
//            roleMapper.insert(user.getRole());
//            userRoleMapper.insert(new UserRole(user.getuId(),user.getRole().getrId()));
        }catch (Exception e){
            System.out.println("添加错了");
        }

    }

    @Override
    public void delete(Integer uId) {
        User user = new User();
        user.setuId(uId);
        User user1 = userMapper.selectOne(user);
        user1.setuStatus(2);
        userMapper.updateByPrimaryKey(user1);
}

    @Override
    public void update(User user) {
        userMapper.updateByPrimaryKey(user);
    }


}
